if (!jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.machines.discovered.DiscoveredInstallations")) {
    /*
    *
    */
    jsx3.Class.defineClass(
            "tibco.admin.enterpriseAssets.machines.discovered.DiscoveredInstallations",
            jsx3.lang.Object,
            null,
            function(discovery) {


                discovery.subscribeAll = function () {
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.machines.search.EventID.ON_SET_DETAILS, discovery.callgetDiscoveredManagementDaemons)
                    tibco.admin.PubSub.subscribe(tibco.admin.enterpriseAssets.machines.search.EventID.ON_CLEAR_CACHE, discovery.cleareCache)
                    //discovery.callgetDiscoveredManagementDaemons();
                }


                //call this method to begin the service call (eg.discovery.callgetDiscoveredManagementDaemons();)
                discovery.callgetDiscoveredManagementDaemons = function(message) {
                    var objService = tibco.admin.Main.loadService("tibco.admin",
                            "tibco/admin/enterpriseAssets/machines/discovered/rules/getDiscoveredManagementDaemonsRequest.xml",
                            "getDiscoveredManagementDaemons",
                            "get discovered Management Daemons");
                    objService.operationHandler = message.operationHandler;
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getManagementDaemonEndpointURL())
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, discovery.ongetDiscoveredManagementDaemonsSuccess);
                    objService.doCall();
                };
                discovery.ongetDiscoveredManagementDaemonsSuccess = function(objEvent) {
                    discovery.printIfIncremented();
                };

                discovery.cleareCache = function() {
                    tibco.admin.util.Log.SERVICE.trace("Clearing the cache discoveredInstallationsCache21B");
                    tibco.admin.Main.getServer().getCache().clearById("discoveredInstallationsCache21B")
                    tibco.admin.Main.getServer().getJSXByName("matrix21B").repaint();

                }

                discovery.printIfIncremented = function() {
                    var matrixList = tibco.admin.Main.getServer().getJSXByName("matrix21B");
                    var response = tibco.admin.Main.getServer().getCache().getDocument("discoveredInstallationsPreCache21B")
                    var listCached = tibco.admin.Main.getServer().getCache().getDocument("discoveredInstallationsCache21B")
                    var itemsInResponse;
                    var itemsInList = 0;
                    if (!response) {
                        tibco.admin.util.Log.SERVICE.warn("Response cache is null");
                        return;
                    }
                    if (listCached) {
                        itemsInList = (dummycollection = listCached.selectNodes("//record"))? dummycollection.getLength():0;
                    }
                    itemsInResponse = (dummycollection = response.selectNodes("//record"))? dummycollection.getLength():0;
                    if (itemsInResponse > itemsInList) {
                        tibco.admin.util.Log.SERVICE.trace("Seting the cache discoveredInstallationsCache21B");
                        tibco.admin.Main.getServer().getCache().setDocument("discoveredInstallationsCache21B", response)
                        matrixList.repaint();
                        // send a message to Search Parameters to update its discoverd installations
                        var msg = new Object();
                        msg.subject = tibco.admin.enterpriseAssets.machines.search.EventID.FOUND_INSTALLATIONS;
                        msg.foundinstallations = itemsInResponse - 1;
                        // ignore the first line, by Bench
                        tibco.admin.PubSub.publish(msg);
                    }

                    var cacheDoc = tibco.admin.Main.getServer().getCache().getDocument(matrixList.getXMLId());
                    var buttonArray = tibco.admin.Main.getServer().getJSXByName("paneLowerBlock21B").getDescendantsOfType(jsx3.gui.Button, false);
                    if (cacheDoc && cacheDoc.getRootNode().selectNodes("//record").getLength() > 0) {
                        for (var i = 1; i < buttonArray.length; i++) { // skip button 0 (Bind)
                            buttonArray[i].setEnabled(jsx3.gui.Form.STATEENABLED, true);
                        }
                    }
                    else {
                        for (var i = 1; i < buttonArray.length; i++) { // skip button 0 (Bind)
                            buttonArray[i].setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                        }
                    }
                    var mtxList = tibco.admin.Main.getServer().getJSXByName('matrix21B');
                    var buttonBind = tibco.admin.Main.getServer().getJSXByName("btnBind21B");
                    if (tibco.admin.Main.getServer().getCache().getDocument(mtxList.getXMLId()).selectNodes("//record[@checked='1']").getLength() == 0) {
                        buttonBind.setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                    }
                    else {
                        buttonBind.setEnabled(jsx3.gui.Form.STATEENABLED, true);
                    }

                }

                discovery.enableBindButton = function(checkBox, intCHECKED, emGetSession) {
                    setTimeout(function() {
                        var buttonBlock = tibco.admin.Main.getServer().getJSXByName("paneLowerBlock21B");
                        var buttonBind = buttonBlock.getDescendantOfName('btnBind21B');
                        var mtxList = tibco.admin.Main.getServer().getJSXByName('matrix21B');
                        if (tibco.admin.Main.getServer().getCache().getDocument(mtxList.getXMLId()).selectNodes("//record[@checked='1']").getLength() == 0) {
                            buttonBind.setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                        }
                        else {
                            buttonBind.setEnabled(jsx3.gui.Form.STATEENABLED, true);
                        }
                    }, 0)

                }

                discovery.filterManagementDaemons = function() {
                    var filtered = tibco.admin.Main.getServer().getCache().getDocument("discoveredInstallationsCache21B").cloneDocument()
                    tibco.admin.util.Log.SERVICE.trace("filtered " + filtered)
                    var configurations = filtered.selectNodes("//record[@configurationName]")
                    var configuration_i
                    for (var i = 0; i < configurations.size(); i++) {
                        configuration_i = configurations.get(i)
                        var hostNames = configuration_i.selectNodes("./record[@checked='1']")
                        if (hostNames.size() == 0) {
                            filtered.removeChild(configuration_i)
                        }
                        else {
                            configuration_i.removeChildren()
                            for (var j = 0; j < hostNames.size(); j++) {
                                configuration_i.appendChild(hostNames.get(j))
                            }
                        }
                    }
                    tibco.admin.util.Log.SERVICE.trace("filtered " + filtered)
                    tibco.admin.util.Log.SERVICE.trace("f------d " + tibco.admin.Main.getServer().getCache().getDocument("discoveredInstallationsCache21B"))

                    tibco.admin.Main.getServer().getCache().setDocument("discoveredInstallationsCache21B_filtered", filtered)
                };

                //call this method to begin the service call (eg.discovery.callconnectToManagementDaemons();)
                discovery.callconnectToManagementDaemons = function() {
                    tibco.admin.util.Log.SERVICE.trace("Connecting to ManagementDaemons.");
                    var objService = tibco.admin.Main.loadService("tibco.admin",
                            "tibco/admin/enterpriseAssets/machines/discovered/rules/connectToManagementDaemons.xml",
                            "connectToManagementDaemons",
                            "connect to Management Daemons");
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getManagementDaemonEndpointURL())
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, discovery.onconnectToManagementDaemonsSuccess);
                    objService.subscribe(jsx3.net.Service.ON_ERROR, discovery.onconnectToManagementDaemonsError);
                    objService.subscribe(jsx3.net.Service.ON_INVALID, discovery.onconnectToManagementDaemonsInvalid);
                    discovery.filterManagementDaemons()
                    //tibco.admin.util.Log.SERVICE.trace(tibco.admin.Main.getServer().getCache().getDocument("discoveredInstallationsCache21B"))
                    objService.doCall();
                };

                discovery.onconnectToManagementDaemonsSuccess = function(objEvent) {
                    var msg = new Object();
                    msg.subject = ("DiscoveredInstallations.ONSETASSOCIATIONS")
                    tibco.admin.PubSub.publish(msg);
                    var closeDialog = discovery.processDocumentAfterConnecting();
                    if (closeDialog) {
                    tibco.admin.Main.getServer().getJSXByName("dlgSearchForInstallations").doClose();
                        } else {
                        tibco.admin.Main.getServer().getJSXByName("matrix21B").repaint();
                    }
                };

                discovery.onconnectToManagementDaemonsError = function(objEvent) {
                };

                discovery.onconnectToManagementDaemonsInvalid = function(objEvent) {
                };


                discovery.filterRecords = function(parentRecord) {
                    var children = parentRecord.getChildNodes();
                    for (var i = 0; i < children.getLength(); i++) {
                        if (children.getItem(i).getAttribute("checked") &&
                            children.getItem(i).getAttribute("checked") == '1') {
                            return true;
                        }
                    }
                    return false;

                }

                discovery.onSelectAll = function() {
                    var mtx = tibco.admin.Main.getServer().getJSXByName("matrix21B");
                    mtxCache = mtx.getXML();
                    var mycollection = mtxCache.selectNodes("//record");
                    while (mycollection.hasNext()) {
                        mycollection.next().setAttribute("checked", "1");
                    }
                    mtx.repaint();
                    var buttonBlock = tibco.admin.Main.getServer().getJSXByName("paneLowerBlock21B");
                    var buttonBind = buttonBlock.getDescendantOfName('btnBind21B');
                    var mtxList = tibco.admin.Main.getServer().getJSXByName('matrix21B');
                    if (tibco.admin.Main.getServer().getCache().getDocument(mtxList.getXMLId()).selectNodes("//record[@checked='1']").getLength() == 0) {
                        buttonBind.setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                    }
                    else {
                        buttonBind.setEnabled(jsx3.gui.Form.STATEENABLED, true);
                    }
                }

                discovery.onDeselectAll = function() {
                    var mtx = tibco.admin.Main.getServer().getJSXByName("matrix21B");
                    mtxCache = mtx.getXML();
                    var mycollection = mtxCache.selectNodes("//record");
                    while (mycollection.hasNext()) {
                        mycollection.next().setAttribute("checked", "0");
                    }
                    mtx.repaint();
                    var buttonBlock = tibco.admin.Main.getServer().getJSXByName("paneLowerBlock21B");
                    var buttonBind = buttonBlock.getDescendantOfName('btnBind21B');
                    var mtxList = tibco.admin.Main.getServer().getJSXByName('matrix21B');
                    if (tibco.admin.Main.getServer().getCache().getDocument(mtxList.getXMLId()).selectNodes("//record[@checked='1']").getLength() == 0) {
                        buttonBind.setEnabled(jsx3.gui.Form.STATEDISABLED, true);
                    }
                    else {
                        buttonBind.setEnabled(jsx3.gui.Form.STATEENABLED, true);
                    }
                }

                discovery.processDocumentAfterConnecting = function() {
                    var newDoc = tibco.admin.Main.getServer().getCache().getDocument("discoveredInstallationsCache21B_response");
                    var discoveryDoc = tibco.admin.Main.getServer().getCache().getDocument("discoveredInstallationsCache21B");
                    var machinesList = discoveryDoc.selectNodes("//record[@checked='1']");
                    var closeDialog = true;
                    for (var i = 0; i < machinesList.size(); i++) {
                        var machinesNode = machinesList.get(i)
                        var record = newDoc.selectSingleNode("//record[@hostName='" + machinesNode.getAttribute("hostName") + "' and @tibcoHome='" + machinesNode.getAttribute("tibcoHome") + "']");
                        if (record.getAttribute("summary")) {
                            record.setAttribute("checked", '1');
                            var parent = machinesNode.getParent();
                            parent.replaceNode(record, machinesNode);
                            closeDialog = false;
                        }
                    }
                    return closeDialog;
                }


            }
            );
}
